package com.cjm.one.coupon.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.result.Result;
import com.cjm.one.common.utils.AuthCustomerContextHolder;
import com.cjm.one.coupon.service.CustomerCouponService;
import com.cjm.one.model.coupon.entity.CouponInfo;
import com.cjm.one.model.coupon.entity.CustomerCoupon;
import com.cjm.one.model.coupon.vo.CouponInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@Slf4j
@Tag(name = "4-客户领取优惠券记录", description = "客户优惠券")
@RequestMapping("/app/customer/coupon")
public class AppCustomerCouponController {
    @Resource
    private CustomerCouponService  customerCouponService;
    @GetMapping("/list")
    @Operation(summary = "查询用户领取的优惠卷信息列表")
    public Result<Page<CouponInfoVo>> getList(@RequestParam Long current,
                                              @RequestParam Long size ,
                                              @RequestParam(name = "keyWord", required = false) String keyWord,
                                              @RequestParam(name = "rangeType", required = false) Integer rangeType) {
        log.debug("查询优惠券信息列表, 当前页码:{}, 页面大小:{}", current, size);
        Long customerId = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        Page<CouponInfoVo> page = customerCouponService.getCouponInfoAppReceivedPage(current, size,keyWord, rangeType,customerId);
        return Result.ok(page);
    }

    @PostMapping("/receive")
    @Operation(summary = "领取优惠券信息")
    public Result<Boolean> save(@RequestBody CustomerCoupon customerCoupon) {
        log.debug("领取优惠券信息:{}",customerCoupon);
        LocalDateTime now = LocalDateTime.now();
        Long id = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        customerCoupon.setCustomerId(id);
        customerCoupon.setReceiveTime( now);
        return Result.ok(customerCouponService.receiveCoupon(customerCoupon));
    }
}
